<?php

namespace App\Http\Controllers\Common;

use \Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Laravel\Passport\Client;
use App\Models\Account\User;
use App\Helpers\Code;
use Illuminate\Http\Request;

class AuthorityController extends Controller
{

    use AuthenticatesUsers;

    public function username()
    {
        return 'phone';
    }

    public function login(Request $request)
    {
        $rules = [
            'phone' => array('bail', 'required', 'regex:' . $this->pregPhone),
            'password' => 'required'
        ];
        $messages = [
            'phone.required' => trans('system.login.phone_required'),
            'password.required' => trans('system.login.password_required'),
            'phone.regex' => trans('user.regex_match'),
        ];

        $credentials = $this->credentials($request);

        $validator = Validator::make($credentials, $rules, $messages);

        if ($validator->fails()) {
            return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
        }

        if ($this->guard('api')->attempt($credentials, $request->input('remember'))) {
            $user = Auth::user();
            if ($user->is_blocked === 1) {
                return $this->error('system.login.account_blocked', Code::SYSTEM_STATUS_ERROR);
            } else {
                $user->setSession($user);

                $content = $this->sendLoginResponse($request)->content();
                $data = json_decode($content);
                $data->user_id = $user->id;
                $this->result['data'] = $data;
                return $this->result;
            }
        } else {
            return $this->error('system.login.account_password_error', Code::SYSTEM_STATUS_ERROR);
        }
    }

    public function logout(Request $request)
    {
        $req = $request->user('api');
        if ($req) {
            event(new \Illuminate\Auth\Events\Logout($req));
            $req->token()->revoke();
        }

        return $this->result;
    }

    protected function sendLoginResponse(Request $request)
    {
        $this->clearLoginAttempts($request);

        return $this->authenticated($request);
    }

    protected function authenticated(Request $request)
    {
        return $this->authenticateClient($request);
    }

    protected function authenticateClient(Request $request)
    {
        $credentials = $this->credentials($request);

        $password_client = Client::query()->where('password_client', 1)->latest()->first();

        $request->request->add([
            'grant_type' => 'password',
//            'client_id' => config('system.passport_client_id'),
//            'client_secret' => config('system.passport_client_secret'),
            'client_id' => $password_client->id,
            'client_secret' => $password_client->secret,
            'username' => $credentials['phone'],
            'password' => $credentials['password'],
            'scope' => ''
        ]);

        $proxy = Request::create('oauth/token', 'POST');

        $response = \Route::dispatch($proxy);

        return $response;
    }

}
